module.exports = function(grunt){

    var setting = require('./setting');
    var config = {
        app_dir: 'app',
        build_dir: 'build',
        release_dir: '../bosch/src/main/webapp'
    };
    // 项目配置
    grunt.initConfig({
        config: config,
        pkg: grunt.file.readJSON('package.json'),
        clean: {
            build: {
                src: [ 'build' ]
            },
        },
        useminPrepare: {
            html: ['<%= config.app_dir %>/**/*.vm'],
            options: {
                dest: "<%= config.build_dir %>"
            }
        },
        usemin:{
            css:{
                files:{
                    src: ['<%= config.build_dir %>/**/*.css']
                }
            },
            js: '<%= config.build_dir %>/**/*.js',
            html: ['<%= config.build_dir %>/*.vm','<%= config.build_dir %>/**/*.vm'],
            options:{
                assetsDirs: ['<%= config.build_dir %>','<%= config.build_dir %>/img', '<%= config.build_dir %>/css', '<%= config.build_dir %>/js'],
                patterns: {
                    html: [
                        [/adminLink\.setTarget\('([^']+.(css|js))'\)"/g, 'replace staticserver', function (p) {
                            return p;
                        }]
                    ]
                }
            }
        },

        copy: {
            build: {
                files: [
                    {expand: true,cwd:'<%= config.app_dir %>/admin',src: '**',dest: '<%= config.build_dir %>/admin',flatten: false,filter: 'isFile'},
                    {expand: true,cwd:'<%= config.app_dir %>',src: [ '*.vm', '!**/*.css' , '!**/*.js' , '!**/*.jpg', '!**/*.png' , '!**/*.jpeg', '!**/*.gif'],ext:'.vm',dest: '<%= config.build_dir %>/',flatten: false,filter: 'isFile'},
                    {expand: true,cwd: '<%= config.app_dir %>/css',src: '**',dest: '<%= config.build_dir %>/css',flatten: false,filter: 'isFile'},
                    {expand: true,cwd: '<%= config.app_dir %>/js',src: '**',dest: '<%= config.build_dir %>/js/',flatten: false,filter: 'isFile'},
                    {expand: true,cwd: '<%= config.app_dir %>/img/',src: '**',dest: '<%= config.build_dir %>/img/',flatten: false,filter: 'isFile'}

                ]
            },
            release: {
                files:[
                    {expand: true,cwd:'<%= config.build_dir %>',src: ['home-layout.vm'], dest: '<%= config.release_dir %>/home/templates/layout/',flatten: true},
                    {expand: true,cwd:'<%= config.build_dir %>',src: ['admin-layout.vm'], dest: '<%= config.release_dir %>/admin/templates/layout/',flatten: true}
                ]
            },
        },
        filerev: {
            css: {
                src: ['<%= config.build_dir %>/css/**/*.css']
            },
            js: {
                src: ['<%= config.build_dir %>/js/**/*.js']
            },
            image: {
                src: ['<%= config.build_dir %>/img/**']
            }
        },
        cdn: {
            options: {
                cdn: setting.cdnDomain,
                flatten: true,
                supportedTypes: { 'ejs': 'vm' }
            },
            dist: {
                src: ['<%= config.build_dir %>/*.vm', '<%= config.build_dir %>/static/*.css'],
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-usemin');
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-filerev');
    grunt.loadNpmTasks('grunt-cdn');

    // 默认任务
    grunt.registerTask('build', ['clean', 'useminPrepare','copy:build', 'filerev','usemin','copy:release']);
    grunt.registerTask('upload', 'Upload files to cdn', function() {
        var done = this.async();

        var cdn = require('./cdn');

        cdn.upload();
    });
}
